package com.itwn.email.producer.g_dlx;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DeadLetterConfig {
    /**
     * - 普通队列： normal.queue
     * - 普通交换机： normal.exchange
     * - 队列绑定到交换机路由键： city.gz
     */
    @Bean
    public Queue normalQueue(){
        //Queue queue = new Queue("normal.queue");

        return QueueBuilder.durable("normal.queue")
                // 绑定私信交换机
                .deadLetterExchange("dlx.exchange")
                // 绑定死信路由key
                .deadLetterRoutingKey("dlx.routing.key")
                // 设置消息过期时间，如果消息20秒没有消费会进入死信队列
                .ttl(20000)
                // 设置队列中最多存放2个消息，超过2个会把最早的消息移动到死信队列
                //.maxLength(2)
                .build();
    }
    @Bean
    public DirectExchange normalExchange(){
        return new DirectExchange("normal.exchange");
    }
    @Bean
    public Binding bindingNormalEQ(
            Queue normalQueue,DirectExchange normalExchange){
        return BindingBuilder.bind(normalQueue).to(normalExchange).with("city.gz");
    }

    /**
     * - 死信队列： dlx.queue
     * - 死信交换机： dlx.exchange
     * - 死信队列绑定到死信交换机路由键： dlx.routing.key
     */
    @Bean
    public Queue dlxQueue(){
        return new Queue("dlx.queue");
    }
    @Bean
    public DirectExchange dlxExchange(){
        return new DirectExchange("dlx.exchange");
    }
    @Bean
    public Binding bindingDlxQueueToExchange(
            Queue dlxQueue,DirectExchange dlxExchange){
        return BindingBuilder.bind(dlxQueue).to(dlxExchange).with("dlx.routing.key");
    }
}
